import re

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import render, redirect
from myapp.models import *
import time

def replace_user_data(user_data):
    print(f" 动态变量替换前 user_data == > {user_data}")

    for key in user_data.keys():
        if '#' in user_data[key]:
            pars = re.findall(r"#(.*?)#", user_data[key])
            # 找到的是个列表
            # print(pars)
            # 从DB_PAR 找到要替换的数据执行的代码/内容/ 函数
            for par in pars:
                code_par = DB_PAR.objects.filter(name=par)[0].code
                try:
                    value = str(eval(code_par))
                except:
                    value = str(code_par)
                # print(value)
                user_data[key] = user_data[key].replace("#%s#"%par, value)


    print(f" 动态变量替换前后 user_data == > {user_data}")
    return user_data

